﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.WCFConsumerTransport;
using System.ServiceModel;


namespace iLangl.Transport
{
	public class WCFConsumer : ITransport
	{
		private String _uri;
		public WCFConsumer(String uri)
		{
			_uri = uri;
		}

		public void SendRequest(UploadRequest request)
		{
			WCFConsumerTransport.UploadRequest urequest = new WCFConsumerTransport.UploadRequest();

			urequest.DeliveredFileName = request.DeliveredFileName;
			urequest.PackageData = request.PackageData;
			urequest.PackageId = request.PackageId;
			urequest.PackageSize = request.PackageSize;
			//UploadRequest
			TransportClient transport = getTransport();
			transport.SendRequest(urequest);
		}

		public UploadLightResponse SendRequestSync(UploadLightRequest request)
		{
			WCFConsumerTransport.UploadLightRequest urequest = new WCFConsumerTransport.UploadLightRequest();

			urequest.DeliveredFileName = request.DeliveredFileName;
			urequest.PackageData = request.PackageData;
			urequest.PackageId = request.PackageId;

			TransportClient transport = getTransport();

			WCFConsumerTransport.UploadLightResponse ulr = transport.SendRequestSync(urequest);


			UploadLightResponse result = new UploadLightResponse();
			result.Responses = ulr.Responses;

			return result;
		}

		private TransportClient getTransport()
		{
			

			BasicHttpBinding binding = new BasicHttpBinding();
			binding.MaxReceivedMessageSize = 104857600;
			EndpointAddress address = new EndpointAddress(_uri); 
			TransportClient transport = new TransportClient(binding, address);


			return transport;
		}
	}
}
